package com.wyg.epdata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyg.epdata.common.constant.BaseConstant;
import com.wyg.epdata.entity.Article;
import com.wyg.epdata.entity.ArticleKw;
import com.wyg.epdata.entity.dto.KwCountDTO;
import com.wyg.epdata.entity.respones.WordsResp;
import com.wyg.epdata.mapper.ArticleKwMapper;
import com.wyg.epdata.service.ArticleKwService;
import com.wyg.epdata.util.HttpUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author WangYongGan
 * @since 2021-11-11
 */
@Component
public class ArticleKwServiceImpl extends ServiceImpl<ArticleKwMapper, ArticleKw> implements ArticleKwService {

    @Override
    public void saveArticleKeyWord(Article article) {
        HashMap<String, String> params = new HashMap<>();
        params.put("text", article.getRawText());
        //获得关键字
        WordsResp wordsResp = (WordsResp) HttpUtil.sendGet(BaseConstant.urlGetWords, params);
        wordsResp.getWords().forEach(word -> {
            ArticleKw articleKw = new ArticleKw();
            articleKw.setArticleId(article.getId());
            articleKw.setKeyword(word);
            baseMapper.insert(articleKw);
        });
    }

    @Override
    public List<KwCountDTO> getNKwByEntity(String entity, int n) {
        List<KwCountDTO> nKwByArticles = baseMapper.getNKwByEntity(entity, n);
        return nKwByArticles;
    }

    @Override
    public List<String> getNKw(int n) {
        List<String> nKw = baseMapper.getNKw(n);
        return nKw;
    }
}
